package com.txc.books.controller;

import com.txc.books.entity.Book;
import com.txc.books.entity.BookArea;
import com.txc.books.exception.BaseException;
import com.txc.books.service.IBookAreaService;
import com.txc.books.utils.PageRequest;
import com.txc.books.utils.PageResult;
import org.springframework.web.bind.annotation.*;

/**
 * @author: XCTANG
 *
 */

@RestController
@RequestMapping("/area")
public class BookAreaController {

    private final IBookAreaService BookAreaService;

    public BookAreaController(IBookAreaService BookAreaService){
        this.BookAreaService = BookAreaService;
    }

    //查找全部区域
    @GetMapping(value = "/findAll")
    public void findAll(){ BookAreaService.findAll();
    }

    //添加区域信息
    @PostMapping(value = "/insert")
    public void addArea(BookArea bookArea){
        try{
            BookAreaService.insert(bookArea);
        }catch (BaseException e){
            System.out.println("出现定义的异常");
        }catch (Exception e){
            System.out.println("出现不可预料的异常");
        }
    }

    //更新区域信息
    @PostMapping(value = "/update")
    public void updateArea(BookArea bookArea){

        try{
            BookAreaService.update(bookArea);
        }catch (BaseException e){
            System.out.println("出现定义的异常");
        }catch (Exception e){
            System.out.println("出现不可预料的异常");
        }
    }


    //删除区域信息
    @GetMapping(value = "/delete")
    public void deleteArea(@RequestParam("id") String id){
        BookAreaService.delete(Integer.parseInt(id.trim()));
    }

    //根据id查询区域信息
    @GetMapping(value = "/select")
    public void selectArea(@RequestParam("id") String id){
        BookAreaService.select(Integer.parseInt(id));
    }

    //分页查询
    @GetMapping(value="/findPage")
    public PageResult findPage(PageRequest pageQuery) {

        return BookAreaService.findByPager(pageQuery);
    }

}
